#pragma once
#include <string>
#include <functional>
#include <workflow/WFTaskFactory.h>
#include <protocol/frpc_msgio.h>
#include <cppbase/utility.h>
#include <frpc/router.h>
namespace Frpc{
class Task;
class Connection
{
public:

};


class Task : public WFComplexClientTask<RequestMsg, ReplyMsg>
{
protected:
	virtual bool check_request();//called by parent,task start when it returns true
	virtual CommMessageOut *message_out();
	virtual CommMessageIn *message_in();
	virtual int keep_alive_timeout();
//	virtual bool init_success(); caled by init_with url(ip)
	virtual bool finish_once();//called in parent::done() when a req-reply done.return true means ready for user cmd.

private:
	std::string username_;
	std::string password_;
	std::string db_;
	std::string res_charset_;
	short character_set_;
	short state_;
	bool is_user_request_;

public:
	virtual WFConnection *get_connection() const
	{
		CommConnection *conn;

		if (this->target)
		{
			conn = this->CommSession::get_connection();
			if (conn)
				return (WFConnection *)conn;
		}

		errno = ENOTCONN;
		return NULL;
	}
	//static void set_ip_port(std::string host,uint16_t port){	}
	//static New()
	Task(task_callback_t && callback):
		WFComplexClientTask(0, std::move(callback))
	{
	char buf[8];
	std::string url = "scheme://";
	ParsedURI uri;
	sprintf(buf, "%u", 8080);
	url += "192.168.106.128";
	url += ":";
	url += buf;
	URIParser::parse(url, uri);
	init(std::move(uri));
	set_transport_type(TT_TCP);
	set_fixed_addr(true);
	set_keep_alive(1000*60);
	set_wait_timeout(1000*3);
	set_receive_timeout(1000*3);
    }
};


class ServerTask : public WFServerTask<RequestMsg, ReplyMsg>
{
public:
	ServerTask(CommService *service,std::function<void (WFNetworkTask<RequestMsg, ReplyMsg> *)>&process):
		WFServerTask(service, WFGlobal::get_scheduler(), process)
	{
		set_keep_alive(1000*60);
			set_wait_timeout(1000*3);
	set_receive_timeout(1000*3);
DLOG("create server task");
	}

virtual ~ServerTask(){
	DLOG("delete server task");
}

protected:

	virtual SubTask *done();
	virtual CommMessageOut *message_out();
	virtual CommMessageIn *message_in();
};



}